const api = require("../../utils/api.js").default;
var token = wx.getStorageSync("token");
// pages/shopcar/shopcar.js
Page({ 
  /**
   * 页面的初始数据
   */
  data: {
    head:"购物车",
    cartList:[],
    cartTotal:[],
    isSelect:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function () {
   

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */

  onShow: function (options) {
    var that = this;
    wx.request({
      //请求购物车
      url: api.cart,
      header: {
        'X-Nideshop-Token': token
      },
      success(res) {
        console.log(res.data.data)
        that.setData({
          cartList: res.data.data.cartList,
          cartTotal: res.data.data.cartTotal,
          
        })
        var isSelect = [];
        for (let i = 0; i < res.data.data.cartList.length; i++){
          isSelect[i] = res.data.data.cartList[i].checked;
        }
        that.setData({
          isSelect: isSelect
        })
      }
    })
  },
  //点击减少商品数量
  reduce(e){
    const that = this;
    var item = e.currentTarget.dataset.num;
    var num= item.number;
    if ( num >=0){
      num--;
    }else{
      return false;
    }
      wx.request({
        url: api.cartupdate,
        method:"POST",
        header: {
          'X-Nideshop-Token': token
        },
        data:{
          id: item.id,
          goodsId: item.goods_id,
          number: num,
          productId: item.product_id
        },
        success(res){
          that.setData({
            cartList: res.data.data.cartList,
            cartTotal: res.data.data.cartTotal,
          })
        }
      })
  },
  //点击增加商品数量
  add(e){
    //拿到当前item的信息
    const that = this;
    var item = e.currentTarget.dataset.num;
    //改变的是商品数量 number
    var num = item.number;
    if (num >= 0) {
      num++;
    }
    wx.request({
      url: api.cartupdate,
      header: {
        'X-Nideshop-Token': token
      },
      method: "POST",
      //请求的参数 购物车id 商品id 数量number 产品id
      data: {
        id: item.id,
        goodsId: item.goods_id,
        number: num,
        productId: item.product_id
      },
      success(res) {
        that.setData({
          //更新购物车列表数据
          cartList: res.data.data.cartList,
          cartTotal: res.data.data.cartTotal,
        })
      }
    })
  },
  //选中或取消
  checkboxChange(e){
    var that = this;
    //获取点击的复选框位置
    var index = e.currentTarget.dataset.index;
    //获取选中的商品的产品id
    var productid = e.currentTarget.dataset.items.product_id;
    //获取选中的商品的checked值
    var checked = e.currentTarget.dataset.items.checked;
    //判断商品在后台是否选中
    if (checked == 1){
      checked = 0;
    }else{
      checked = 1;
    }
    //将后台选中状态赋值给前端数据，并渲染到页面
    that.data.isSelect[index] = checked
    //请求选择接口
    wx.request({
      url: api.selectgoods,
      method:"POST",
      header: {
        'X-Nideshop-Token': token
      },
      data: {
        productIds: productid,
        isChecked: checked
      },
      success(res){
        var isSelect = [];
        // 更新isSelect数组数据
        for (let i = 0; i < res.data.data.cartList.length; i++) {
          isSelect[i] = res.data.data.cartList[i].checked;
        }
        // 更新data数据
        that.setData({
          cartList: res.data.data.cartList,
          cartTotal: res.data.data.cartTotal,
          isSelect: isSelect
        })
      }
    })
  },
  //点击全选
  selectAll(){
    var that = this;
    var productid =[];
    // 获取商品的产品id，并赋值给productid数组
    for (let i = 0; i < this.data.cartList.length ; i++ ){
      productid[i] = this.data.cartList[i].product_id;
    }
    // 定义checked初始状态
    var checked = 1;

    // 获取isSelect数组
    var isSelect = this.data.isSelect;
    // 遍历isSelect数组
    for (let i = 0; i < isSelect.length ; i++){
      // 判断isSelect内是否存在0
      // 若存在，将数组内所有元素设置为1，将checked设置为1，并跳出循环
      // 若不存在，将数组内所有元素设置为0，将checked设置为0
      if (isSelect[i] == 0 ){
        for (let j = 0; j < isSelect.length ; j++){
          isSelect[j] = 1;
        }
        checked = 1;
        break;
      }else{
        isSelect[i] = 0;
        checked = 0;
      }
    }
    // 将isSelect更新到data,更新页面选中状态
    this.setData({
      isSelect: isSelect
    })
    //字符串  产品id
    // 将productid数组元素用逗号连接形成字符串
    var productIds = productid.join();
    // 请求选择接口
    wx.request({
      url: api.selectgoods,
      method: "POST",
      header: {
        'X-Nideshop-Token': token
      },
      data: {
        productIds: productIds,
        isChecked: checked
      },
      success(res) {
        // 更新data数据
        that.setData({
          cartList: res.data.data.cartList,
          cartTotal: res.data.data.cartTotal,
          isSelect: that.data.isSelect
        })
      }
    })
  },

  //删除
  cancelList(e){
    var that = this;
    //产品id是字符串类型
    var item = String(e.currentTarget.dataset.item.product_id);
    wx.request({
      url: api.deleteList,
      method: "POST",
      header: {
        'X-Nideshop-Token': token
      },
      //请求的参数 产品id
      data: {
        productIds: item
      },
      success(res) {
        that.setData({
          //更新购物车列表数据
          cartList: res.data.data.cartList,
          cartTotal: res.data.data.cartTotal,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //结算，跳转到支付页面
  addMoney(){
    wx.navigateTo({
      url: '../address/address',
      success(res){
        console.log(res);
      }
    })
  }
})